package com.zimmor.annotation;

import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @Author:zimmor
 * @Date: 2021/11/8 13:56
 * @Description 项目启动类
 * @version: 1.0.0
 */

public class BeanAnnotationInit {
    public static void main(String[] args) throws Exception{
        //创建springBean容器
        HashMap<String, Object> springMap = new HashMap();
        //扫描到所有有@Bean注解的方法
        Class<BeanDemo> beanDemoClass = BeanDemo.class;
        Method[] declaredMethods = beanDemoClass.getDeclaredMethods();
        //遍历所有的方法实例，得到方法的返回值
        for (Method declaredMethod : declaredMethods) {
            boolean annotationPresent = declaredMethod.isAnnotationPresent(Bean.class);
            if(annotationPresent) {
                //把返回值放入容器SpringMap中
                Class<?> beanClass = declaredMethod.getReturnType();
                Object bean = beanClass.getConstructor().newInstance();
                String typeName = beanClass.getTypeName();
                String[] split = typeName.split("\\.");
                String key = split[split.length - 1];
                key = key.substring(0,1).toLowerCase() + key.substring(1);
                springMap.put(key, bean);

            }
        }
        System.out.println(springMap.toString());
    }
}
